package com.springboot.redis.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @AUTHOR: yb.feng
 * @DATE: 2020/12/2 21:26
 * @DESC:
 */
@Configuration
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "spring.redis.cluster")
public class RedisConfig {

    private List<String> nodes;

    public List<String> getNodes() {
        return nodes;
    }

    public void setNodes(List<String> nodes) {
        this.nodes = nodes;
    }

    @Bean
    public JedisCluster jedisCluster(){
        Set<HostAndPort> hostAndPorts = new HashSet<>();
        for (String node : nodes) {
            String[] split = node.split(":");
            hostAndPorts.add(new HostAndPort(split[0], Integer.parseInt(split[1])));
        }
        return new JedisCluster(hostAndPorts, new JedisPoolConfig());
    }
}
